﻿using adb;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PowerStation.AppCode;
using PowerStation.Controllers;
using PowerStation.RequestModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Z.EntityFramework.Plus;

namespace PowerStation.Controllers
{
    /// <summary>
    /// 通知,事件
    /// </summary>
    public class NoticationController : BaseApi<NoticationController>
    {
        private IMqttModel mqtt;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="mqtt"></param>
        public NoticationController(IHttpContextAccessor content, IMqttModel mqtt) : base(content)
        {
            this.mqtt = mqtt;
        }









        /// <summary>
        /// 返回未读信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultApi<List<StatisticsModel>>> NoReadList()
        {
            var lst = await this._db.tMessage.Where(x=>x.messageOfUser_userId_fk == this.userId && x.isRead == false).GroupBy(x=>x.messageType,(x,k)=>new StatisticsModel() { 
                title = x,
                data = k.Count(),
                data_tb = 0
            }).ToListAsync();
            return ResultApi.ok<List<StatisticsModel>>(lst, lst.Count);
        }




        /// <summary>
        /// 通知列表,返回接收到的通知
        /// </summary>
        [HttpGet]

        public async Task<ResultApi<List<tMessage>>> NoticationList([FromQuery]PageModel model, [FromQuery] bool? isRead = null)
        {

            IQueryable<tMessage> lst = this._db.tMessage.Where(x=>x.messageOfUser_userId_fk == this.userId).OrderBy(x=>x.isRead);
            if (isRead.HasValue)
            {
                lst.Where(x => x.isRead == isRead.Value);
            }
            lst = lst.OrderByDescending(x => x.addTime);
            var result = await model.setPageAsync(lst);
            return ResultApi.ok(await result.Item2.ToListAsync(),result.Item1);
        }
        /// <summary>
        /// 设置通知为已读
        /// </summary>
        /// <param name="noticationId">通知Id</param>
        /// <param name="powerStationId">电站Id和通知Id二选一, 传入电站Id则设置该电站下的警报为已读</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultApi<bool>> NoticationRead(int noticationId,long? powerStationId = null)
        {
            if(powerStationId.HasValue)
                await this._db.tMessage.Where(x =>x.messageOfUser_userId_fk == this.userId && x.messageOfPowerStation_powerStationId_fk == powerStationId).UpdateAsync(x => new tMessage()
                {
                    isRead = true,
                });
            else
                await this._db.tMessage.Where(x => x.messageId == noticationId && x.messageOfUser_userId_fk == this.userId).UpdateAsync(x => new tMessage()
                {
                    isRead = true,
                });
            await this._db.SaveChangesAsync();
            return ResultApi.ok(false);
        }

        /// <summary>
        /// 全部设置已读
        /// </summary>
        [HttpPut]
        public async Task<ResultApi<bool>> NoticationReadAll()
        {

            await this._db.tMessage.Where(x => x.isRead == false && x.messageOfUser_userId_fk == this.userId).UpdateAsync(x => new tMessage()
            {
                isRead = true,
            });
            await this._db.SaveChangesAsync();
            return ResultApi.ok(false);
        }



        /// <summary>
        /// 删除通知
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultApi<bool>> Delete(int messageId)
        {
            await this._db.tMessage.Where(x => x.messageOfUser_userId_fk == this.userId && x.messageId == messageId).DeleteAsync();
            return ResultApi.okOrError<bool>(true, true);
        }





        /// <summary>
        /// 获取WebSocket路径,连接成功后,收到通知就更新NoticationList接口数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultApi<string> GetWebSocketUrl() {
            return ResultApi.ok(this.configuart["webSocketUrl"]);
        }
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="type">通知类型,LC表示人数,CW表示财务信息,XSD表示销售单</param>
        /// <param name="msg">通知内容</param>
        /// <returns></returns>
        [HttpPost]
        public ResultApi<bool> SendNoticotionToUser(int userId,MsgType type,string msg)
        {
            
            return ResultApi.ok(true);
        }
    }
}
